package cs348a.opengl;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;

/**
 * <code>OpenGLLight</code> does the work for OpenGL lighting.
 * 
 * @author H.S Kim
 */
public class JoglLight {
	/** The light model */
	private JoglLightModel model;
	
	/**
	 * Constructor
	 * 
	 * @param model
	 */
	public JoglLight(JoglLightModel model){
		this.model = model;
	}
	
	/**
	 * Constructor
	 * 
	 * @param id
	 */
	public JoglLight(int id){
		model = new JoglLightModel(id);
	}
	
	/**
	 * Returns the light model.
	 * 
	 * @return
	 */
	public JoglLightModel getLightModel(){
		return model;
	}
	
	/**
	 * Initializes the light. Call in the init() method of the GL renderer
	 * 
	 * @param drawable
	 */
	public void initLight(GLAutoDrawable drawable){
		GL gl = drawable.getGL();
		
		gl.glLightfv(model.id(), GL.GL_AMBIENT, model.ambient(), 0);
        gl.glLightfv(model.id(), GL.GL_DIFFUSE, model.diffuse(), 0);
        gl.glLightfv(model.id(), GL.GL_SPECULAR, model.specular(), 0);
        gl.glLightfv(model.id(), GL.GL_POSITION, model.position(), 0);      
        gl.glEnable(model.id());
	}
	
	/**
	 * Sets the light in the display() method of the GL renderer.
	 * 
	 * @param drawable
	 */
	public void displayLight(GLAutoDrawable drawable){
		GL gl = drawable.getGL();
		
		if(model.isEnabled()){
			gl.glEnable(GL.GL_LIGHTING);
		} else {
			gl.glDisable(GL.GL_LIGHTING);
		}
	}
}
